package threadTest.threadPool;

import threadTest.callable.Window2;
import threadTest.runabble.Window1;

import java.util.concurrent.*;

/**
 * @author zyc
 * @create 2022--05--16 10:50
 */
public class PoolDemo {
    private static final int CORE_POOL_SIZE = 5;
    private static final int MAX_POOL_SIZE = 10;
    private static final int QUEUE_CAPACITY = 10;
    private static final Long KEEP_ALIVE_TIME = 1L;

    public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                CORE_POOL_SIZE,
                MAX_POOL_SIZE,
                KEEP_ALIVE_TIME,
                TimeUnit.MINUTES,
                new ArrayBlockingQueue<>(QUEUE_CAPACITY),
                new ThreadPoolExecutor.CallerRunsPolicy());

        Future[] submits = new Future[10];
        for (int i = 0; i < 10; i++) {
            submits[i] = threadPoolExecutor.submit(new Window2());
        }

        for (int i = 0; i < 10; i++) {
            try {
                System.out.println("第【" + i + "】次结果为" + submits[i].get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }

        threadPoolExecutor.shutdown();
        while (!threadPoolExecutor.isTerminated()) {
        }
        System.out.println("结束了所有线程");
    }
}
